package com.tianji.promotion.utils;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class PermuteMyUtil{
    public static<T> List<List<T>> permute(List<T> lst) {
        LinkedList<T> path = new LinkedList<>();
        List<List<T>> res = new ArrayList<>();
        backtrack(lst, res, path);
        return res;
    }

    private static <T> void backtrack(List<T> lst, List<List<T>> res, LinkedList<T> path) {
        if (path.size() == lst.size()) {
            res.add(new ArrayList<>(path));
            return;
        }
        for (int i = 0; i < lst.size(); i++) {
            if (path.contains(lst.get(i)))continue;
            path.add(lst.get(i));
            backtrack(lst, res, path);
            path.removeLast();
        }
    }

    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        List<List<Integer>> permute = permute(list);
        for (List<Integer> integers : permute) {
            System.out.println(integers);
        }
    }
}
